package com.example.lyl.yuejian.view.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.adapter.HomePicAdapter;
import com.example.lyl.yuejian.adapter.HomeVpAdapter;
import com.example.lyl.yuejian.view.home.MasterFragment;
import com.example.lyl.yuejian.view.home.MatchFragment;
import com.hejunlin.superindicatorlibray.CircleIndicator;
import com.hejunlin.superindicatorlibray.LoopViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    Unbinder unbinder;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.app_barlayout)
    AppBarLayout appBarlayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.main_content)
    CoordinatorLayout mainContent;
    @BindView(R.id.looperviewpager)
    LoopViewPager looperviewpager;
    @BindView(R.id.indicator)
    CircleIndicator indicator;
    @BindView(R.id.head_layout)
    FrameLayout headLayout;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        toolbar.setTitle("首页");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

        looperviewpager.setAdapter(new HomePicAdapter(getActivity()));
        looperviewpager.setLooperPic(true);//5s自动轮播
        indicator.setViewPager(looperviewpager); //indicator需要知道viewpager


        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MatchFragment());
        fragments.add(new MasterFragment());
        HomeVpAdapter homeVpAdapter = new HomeVpAdapter(getChildFragmentManager(), fragments);
        viewpager.setAdapter(homeVpAdapter);
        tablayout.setupWithViewPager(viewpager);
        tablayout.setBackgroundColor(Color.WHITE);
        tablayout.setTabTextColors(Color.BLACK, Color.parseColor("#4590e1"));
        String[] titles = {"最近比赛", "热门大师"};

        //添加标题
        for (int i = 0; i < fragments.size(); i++) {
            tablayout.getTabAt(i).setText(titles[i]);
        }



    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
